package cn.edu.scau.cmi.liujianxun.client.fxUI.springhibernate;

import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.liujianxun.springhibernate.dao.*;
import cn.edu.scau.cmi.liujianxun.springhibernate.domain.*;
import cn.edu.scau.cmi.liujianxun.springhibernate.util.ComprehensionApplicationContextUtil;
import javafx.event.ActionEvent;

import javafx.scene.control.TextArea;

import javafx.scene.control.ComboBox;

public class SpringHibernateDemoController {
	@FXML
	private TextArea taResult;
	@FXML
	private ComboBox<String> cb1;
	@FXML
	private TextField tbName1;
	@FXML
	private Button btAdd1;
	@FXML
	private Button btchangeTutor2;
	@FXML
	private Button btShow2;
	@FXML
	private TextField tbTutor2;
	@FXML
	private TextField tbID2;
	@FXML
	private Button btCheck2;

	private static TeacherDAO tDao;
	private static StudentDAO sDao;
	
	public void init() {
		String s[] = {"教师", "学生"};
		cb1.getItems().addAll(s);
		//（1） 初始化applicationContext，并获取studentDAO对象
		ApplicationContext applicationContext = ComprehensionApplicationContextUtil.getApplicationContext();
		sDao = (StudentDAO) applicationContext.getBean("studentDAO");
		tDao = (TeacherDAO) applicationContext.getBean("teacherDAO");
	}
	
	public void btAdd1Event(ActionEvent event) {
		String type = cb1.getValue();
		if(type.equals("教师")) {
        	Teacher t = new Teacher();
        	t.setName(tbName1.getText());
        	tDao.save(t);
        	taResult.appendText("添加成功！   工号：" + t.getId() + "   姓名：" + t.getName() + "\n");
        }
        else if(type.equals("学生")) {
        	Student s = new Student();
        	s.setName(tbName1.getText());
        	sDao.save(s);
        	taResult.appendText("添加成功！   学号：" + s.getId() + "   姓名：" + s.getName() + "\n");
        }
	}
	
	public void btchangeTutor2Event(ActionEvent event) {
		Student s = sDao.findById(Long.valueOf(tbID2.getText()));
		if(s == null) {
			taResult.appendText("该学生不存在！\n");
			return;
		}
		Teacher t = tDao.findById(Long.valueOf(tbTutor2.getText()));
		if(t == null) {
			taResult.appendText("该教师不存在！\n");
			return;
		}
		s.setTeacher(t);
		sDao.update(s);
        taResult.appendText("修改成功！    学号：" + s.getId() + "   姓名：" + s.getName()
        					+ "   导师：" + s.getTeacher().getName() + "\n");
	}

	public void btShow2Event(ActionEvent event) {
		Student s = sDao.findById(Long.valueOf(tbID2.getText()));
		if(s == null) {
			taResult.appendText("该学生不存在！\n");
			return;
		}
		else {
			taResult.appendText("学号：" + s.getId() + "   姓名：" + s.getName());
			if(s.getTeacher() != null)
            	taResult.appendText("   导师：" + s.getTeacher().getName() + "\n");
            else
            	taResult.appendText("   导师：无\n");
		}
	}

	public void btCheck2Event(ActionEvent event) {
		Teacher t = tDao.findById(Long.valueOf(tbTutor2.getText()));
		if(t == null) {
			taResult.appendText("该教师不存在！\n");
			return;
		}
		else {
			taResult.appendText("工号：" + t.getId() + "   姓名：" + t.getName() + "\n");
		}
	}
}
